package io.karim.materialtabs.sample;

import io.karim.materialtabs.sample.dialog.ConstandCodes;
import io.karim.materialtabs.sample.dialog.CustomDialog;
import ohos.agp.components.*;
import ohos.app.Context;

/**
 * TabsSettingsFragment
 *
 * @author hal
 * @since 2021/7/16
 */
public class TabsSettingsFragment extends StackLayout implements Component.ClickedListener, ResettableFragment {
    public static final String INDICATOR_COLOR = "INDICATOR_COLOR";
    public static final String UNDERLINE_COLOR = "UNDERLINE_COLOR";
    public static final String INDICATOR_HEIGHT = "INDICATOR_HEIGHT";
    public static final String UNDERLINE_HEIGHT = "UNDERLINE_HEIGHT";
    public static final String TAB_PADDING = "TAB_PADDING";
    public static final String PADDING_MIDDLE = "PADDING_MIDDLE";
    public static final String SAME_WEIGHT_TABS = "SAME_WEIGHT_TABS";
    public static final String TEXT_ALL_CAPS = "TEXT_ALL_CAPS";
    public static final String TAB_BACKGROUND = "TAB_BACKGROUND";
    public static final String TOOLBAR_BACKGROUND = "TOOLBAR_BACKGROUND";
    public static final String TEXT_COLOR_UNSELECTED = "TEXT_COLOR_UNSELECTED";
    public static final String TEXT_COLOR_SELECTED = "TEXT_COLOR_SELECTED";
    public static final String SHOW_TOOLBAR = "SHOW_TOOLBAR";
    public static final String NUMBER_OF_TABS = "NUMBER_OF_TABS";

    private static final int UNDERLINE_HEIGHT_DEFAULT_DP = 0;
    private static final int INDICATOR_HEIGHT_DEFAULT_DP = 2;
    private static final int TAB_PADDING_DEFAULT_DP = 12;
    private static final int NUMBER_OF_TABS_DEFAULT = 3;
    // Indicator Height
    Slider numberOfTabsSeekBar;
    Text numberOfTabsTextView;

    // Indicator Color
    RadioContainer indicatorColorRadioGroup;
    RadioButton indicatorColorButtonWhite;

    // Underline Color
    RadioContainer underlineColorRadioGroup;
    RadioButton underlineColorButtonMantis;

    // Indicator Height
    Slider indicatorHeightSeekBar;
    Text indicatorHeightTextView;

    // Underline Height
    Slider underlineHeightSeekBar;
    Text underlineHeightTextView;

    // Tab Padding Left Right
    Slider tabPaddingSeekBar;
    Text tabPaddingTextView;

    // Padding Middle
    Checkbox paddingMiddleCheckBox;

    // Should Expand
    Checkbox sameWeightTabsCheckBox;

    // Text All Caps
    Checkbox textAllCapsCheckBox;

    // Show Toolbar
    Checkbox showToolbarCheckBox;

    // Tab Text Color
    RadioContainer tabTextColorRadioGroup;
    RadioButton tabTextColorButtonWhite;

    // Tab Text Selected Color
    RadioContainer tabTextSelectedColorRadioGroup;
    RadioButton tabTextSelectedColorButtonWhite;

    // Tab Background Color
    RadioContainer tabBackgroundColorRadioGroup;
    RadioButton tabBackgroundColorButtonFireEngineRed;

    // Toolbar Background Color
    RadioContainer toolbarColorRadioGroup;
    RadioButton toolbarColorButtonFireEngineRed;

    int underlineHeightDp;
    int indicatorHeightDp;
    int tabPaddingDp;
    int numberOfTabs;
    private MainAbility ability;

    public TabsSettingsFragment(Context context) {
        this(context, null);
    }

    public TabsSettingsFragment(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public TabsSettingsFragment(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initView();
    }

    private void initView() {
        Component parse = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_fraction_tabs_layout, this, false);
        addComponent(parse);
        if (getContext() instanceof MainAbility) {
            ability = (MainAbility) getContext();
        }
        numberOfTabsSeekBar = (Slider) parse.findComponentById(ResourceTable.Id_numberOfTabsSeekBar);
        indicatorHeightSeekBar = (Slider) parse.findComponentById(ResourceTable.Id_indicatorHeightSeekBar);
        underlineHeightSeekBar = (Slider) parse.findComponentById(ResourceTable.Id_underlineHeightSeekBar);
        tabPaddingSeekBar = (Slider) parse.findComponentById(ResourceTable.Id_tabPaddingSeekBar);
        numberOfTabsTextView = (Text) parse.findComponentById(ResourceTable.Id_numberOfTabsTextView);
        indicatorHeightTextView = (Text) parse.findComponentById(ResourceTable.Id_indicatorHeightText);
        underlineHeightTextView = (Text) parse.findComponentById(ResourceTable.Id_underlineHeightText);
        tabPaddingTextView = (Text) parse.findComponentById(ResourceTable.Id_tabPaddingText);
        indicatorColorRadioGroup = (RadioContainer) parse.findComponentById(ResourceTable.Id_indicatorColorRadioGroup);
        indicatorColorButtonWhite = (RadioButton) parse.findComponentById(ResourceTable.Id_indicatorColorButtonWhite);
        paddingMiddleCheckBox = (Checkbox) parse.findComponentById(ResourceTable.Id_paddingMiddleCheckBox);
        underlineColorRadioGroup = (RadioContainer) parse.findComponentById(ResourceTable.Id_underlineColorRadioGroup);
        underlineColorButtonMantis = (RadioButton) parse.findComponentById(ResourceTable.Id_underlineColorButtonMantis);
        sameWeightTabsCheckBox = (Checkbox) parse.findComponentById(ResourceTable.Id_sameWeightTabsCheckBox);
        textAllCapsCheckBox = (Checkbox) parse.findComponentById(ResourceTable.Id_textAllCapsCheckBox);
        showToolbarCheckBox = (Checkbox) parse.findComponentById(ResourceTable.Id_showToolbarCheckBox);
        tabTextColorRadioGroup = (RadioContainer) parse.findComponentById(ResourceTable.Id_tabtext_colorRadioGroup);
        tabTextColorButtonWhite = (RadioButton) parse.findComponentById(ResourceTable.Id_tabtext_colorButtonWhite);
        tabTextSelectedColorRadioGroup = (RadioContainer) parse.findComponentById(ResourceTable.Id_tabTextSelectedColorRadioGroup);
        tabTextSelectedColorButtonWhite = (RadioButton) parse.findComponentById(ResourceTable.Id_tabTextSelectedColorButtonWhite);
        tabBackgroundColorRadioGroup = (RadioContainer) parse.findComponentById(ResourceTable.Id_tabbackground_elementColorRadioGroup);
        tabBackgroundColorButtonFireEngineRed = (RadioButton) parse.findComponentById(ResourceTable.Id_tabbackground_elementColorButtonFireEngineRed);
        toolbarColorRadioGroup = (RadioContainer) parse.findComponentById(ResourceTable.Id_toolbarColorRadioGroup);
        toolbarColorButtonFireEngineRed = (RadioButton) parse.findComponentById(ResourceTable.Id_toolbarColorButtonFireEngineRed);
        setupAndReset();
        setClicked();
    }

    private void setClicked() {
        findComponentById(ResourceTable.Id_tabPaddingInfoButton).setClickedListener(this);
        findComponentById(ResourceTable.Id_tabbackground_elementColorInfoButton).setClickedListener(this);
        findComponentById(ResourceTable.Id_tabtext_colorInfoButton).setClickedListener(this);
        findComponentById(ResourceTable.Id_tabTextSelectedColorInfoButton).setClickedListener(this);
        findComponentById(ResourceTable.Id_toolbarColorInfoButton).setClickedListener(this);
        findComponentById(ResourceTable.Id_textAllCapsInfoButton).setClickedListener(this);
        findComponentById(ResourceTable.Id_underlineColorInfoButton).setClickedListener(this);
        findComponentById(ResourceTable.Id_underlineHeightInfoButton).setClickedListener(this);
        findComponentById(ResourceTable.Id_indicatorColorInfoButton).setClickedListener(this);
        findComponentById(ResourceTable.Id_indicatorHeightInfoButton).setClickedListener(this);
        findComponentById(ResourceTable.Id_paddingMiddleInfoButton).setClickedListener(this);
        findComponentById(ResourceTable.Id_sameWeighTabsInfoButton).setClickedListener(this);
        findComponentById(ResourceTable.Id_showToolbarInfoButton).setClickedListener(this);
        findComponentById(ResourceTable.Id_numberOfTabsInfoButton).setClickedListener(this);
    }

    @Override
    public void setupAndReset() {
        setDefault();
        setTabs();
        setDp();
        setPad();
        indicatorColorClicked();
        underlineColorClicked();
        tabBackgroundColorClicked();
        toolbarColorClicked();
        tabTextColorClicked();
        tabTextSelectedColorClicked();
        indicatorColorButtonWhite.setChecked(true);
        underlineColorButtonMantis.setChecked(true);

        tabTextColorButtonWhite.setChecked(true);
        tabTextSelectedColorButtonWhite.setChecked(true);

        tabBackgroundColorButtonFireEngineRed.setChecked(true);
        toolbarColorButtonFireEngineRed.setChecked(true);

        // Text Style Unselected
        sameWeightTabsCheckBox.setCheckedStateChangedListener((absButton, isChecked) ->
                ability.startTabsAbilityIntent.setParam(SAME_WEIGHT_TABS, isChecked));

        // Text Style Unselected
        paddingMiddleCheckBox.setCheckedStateChangedListener((absButton, isChecked) ->
                ability.startTabsAbilityIntent.setParam(PADDING_MIDDLE, isChecked));

        // Text Style Unselected
        textAllCapsCheckBox.setCheckedStateChangedListener((absButton, isChecked) ->
                ability.startTabsAbilityIntent.setParam(TEXT_ALL_CAPS, isChecked));

        // Text Style Unselected
        showToolbarCheckBox.setCheckedStateChangedListener((absButton, isChecked) ->
                ability.startTabsAbilityIntent.setParam(SHOW_TOOLBAR, isChecked));

        sameWeightTabsCheckBox.setChecked(true);
        paddingMiddleCheckBox.setChecked(false);
        textAllCapsCheckBox.setChecked(true);
        showToolbarCheckBox.setChecked(true);
    }

    private void tabTextSelectedColorClicked() {
        // Text Color Selected
        tabTextSelectedColorRadioGroup.setMarkChangedListener((radioContainer, checkedId) -> {
            String key = TEXT_COLOR_SELECTED;
            switch (checkedId) {
                case 0:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_fire_engine_red);
                    break;
                case 2:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_gorse);
                    break;
                case 3:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_iris_blue);
                    break;
                case 4:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_safety_orange);
                    break;
                case 5:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_white);
                    break;
                case 6:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_black);
                    break;
                case 1:
                default:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_mantis);
                    break;
            }
        });
    }

    private void tabTextColorClicked() {
        // Text Color Unselected
        tabTextColorRadioGroup.setMarkChangedListener((radioContainer, checkedId) -> {
            String key = TEXT_COLOR_UNSELECTED;
            switch (checkedId) {
                case 0:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_fire_engine_red);
                    break;
                case 2:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_gorse);
                    break;
                case 3:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_iris_blue);
                    break;
                case 4:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_safety_orange);
                    break;
                case 5:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_white);
                    break;
                case 6:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_black);
                    break;
                case 1:
                default:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_mantis);
                    break;
            }
        });
    }

    private void toolbarColorClicked() {
        // Toolbar Background Color
        toolbarColorRadioGroup.setMarkChangedListener((radioContainer, checkedId) -> {
            String key = TOOLBAR_BACKGROUND;
            switch (checkedId) {
                case 0:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_fire_engine_red);
                    break;
                case 2:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_gorse);
                    break;
                case 3:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_iris_blue);
                    break;
                case 4:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_safety_orange);
                    break;
                case 5:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_white);
                    break;
                case 6:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_black);
                    break;
                case 1:
                default:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_mantis);
                    break;
            }
        });
    }

    private void tabBackgroundColorClicked() {
        // Tab Background Color
        tabBackgroundColorRadioGroup.setMarkChangedListener((radioContainer, checkedId) -> {
            String key = TAB_BACKGROUND;
            switch (checkedId) {
                case 0:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_fire_engine_red);
                    break;
                case 2:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_gorse);
                    break;
                case 3:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_iris_blue);
                    break;
                case 4:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_safety_orange);
                    break;
                case 5:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_white);
                    break;
                case 6:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_black);
                    break;
                case 1:
                default:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_mantis);
                    break;
            }
        });
    }

    private void underlineColorClicked() {
        // Underline Color
        underlineColorRadioGroup.setMarkChangedListener((radioContainer, checkedId) -> {
            String key = UNDERLINE_COLOR;
            switch (checkedId) {
                case 0:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_fire_engine_red);
                    break;
                case 2:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_gorse);
                    break;
                case 3:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_iris_blue);
                    break;
                case 4:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_safety_orange);
                    break;
                case 5:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_white);
                    break;
                case 6:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_black);
                    break;
                case 1:
                default:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_mantis);
                    break;
            }
        });
    }

    private void indicatorColorClicked() {
        // Indicator Color
        indicatorColorRadioGroup.setMarkChangedListener((radioContainer, checkedId) -> {
            String key = INDICATOR_COLOR;
            switch (checkedId) {
                case 0:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_fire_engine_red);
                    break;
                case 2:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_gorse);
                    break;
                case 3:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_iris_blue);
                    break;
                case 4:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_safety_orange);
                    break;
                case 5:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_white);
                    break;
                case 6:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_black);
                    break;
                case 1:
                default:
                    ability.startTabsAbilityIntent.setParam(key, ResourceTable.Color_mantis);
                    break;
            }
        });
    }

    private void setPad() {
        tabPaddingDp = TAB_PADDING_DEFAULT_DP;
        tabPaddingTextView.setText(getContext().getString(ResourceTable.String_tab_padding) + ": " + tabPaddingDp + "dp");
        tabPaddingSeekBar.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean fromUser) {
                tabPaddingDp = progress;
                tabPaddingTextView.setText(getContext().getString(ResourceTable.String_tab_padding) + ": " + tabPaddingDp + "dp");
                ability.startTabsAbilityIntent.setParam(TAB_PADDING, tabPaddingDp);
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });

        underlineHeightSeekBar.setProgressValue(underlineHeightDp);
        numberOfTabsSeekBar.setProgressValue(numberOfTabs);
        indicatorHeightSeekBar.setProgressValue(indicatorHeightDp);
        tabPaddingSeekBar.setProgressValue(tabPaddingDp);
    }

    private void setDp() {
        indicatorHeightDp = INDICATOR_HEIGHT_DEFAULT_DP;
        indicatorHeightTextView.setText(getContext().getString(ResourceTable.String_indicator_height) + ": " + indicatorHeightDp + "dp");
        indicatorHeightSeekBar.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean fromUser) {
                indicatorHeightDp = progress;
                indicatorHeightTextView.setText(getContext().getString(ResourceTable.String_indicator_height) + ": " + indicatorHeightDp + "dp");
                ability.startTabsAbilityIntent.setParam(INDICATOR_HEIGHT, indicatorHeightDp);
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });
    }

    private void setTabs() {
        numberOfTabs = NUMBER_OF_TABS_DEFAULT;
        numberOfTabsTextView.setText(getContext().getString(ResourceTable.String_number_of_tabs) + ": " + numberOfTabs);
        numberOfTabsSeekBar.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean fromUser) {
                numberOfTabs = progress + 1;
                numberOfTabsTextView.setText(getContext().getString(ResourceTable.String_number_of_tabs) + ": " + numberOfTabs);
                ability.startTabsAbilityIntent.setParam(NUMBER_OF_TABS, numberOfTabs);
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });
    }

    private void setDefault() {
        underlineHeightDp = UNDERLINE_HEIGHT_DEFAULT_DP;
        underlineHeightTextView.setText(getContext().getString(ResourceTable.String_underline_height) + ": " + underlineHeightDp + "dp");
        underlineHeightSeekBar.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean fromUser) {
                underlineHeightDp = progress;
                underlineHeightTextView.setText(getContext().getString(ResourceTable.String_underline_height) + ": " + underlineHeightDp + "dp");
                ability.startTabsAbilityIntent.setParam(UNDERLINE_HEIGHT, underlineHeightDp);
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_tabPaddingInfoButton:
                showDialog(getContext().getString(ResourceTable.String_tab_padding), getContext().getString(ResourceTable.String_tab_padding_details));
                break;
            case ResourceTable.Id_tabbackground_elementColorInfoButton:
                showDialog(getContext().getString(ResourceTable.String_tab_background_color), getContext().getString(ResourceTable.String_tab_background_color_details));
                break;
            case ResourceTable.Id_tabtext_colorInfoButton:
                showDialog(getContext().getString(ResourceTable.String_tab_text_color), getContext().getString(ResourceTable.String_tab_text_color_details));
                break;
            case ResourceTable.Id_tabTextSelectedColorInfoButton:
                showDialog(getContext().getString(ResourceTable.String_tab_text_selected_color), getContext().getString(ResourceTable.String_tab_text_selected_color_details));
                break;
            case ResourceTable.Id_toolbarColorInfoButton:
                showDialog(getContext().getString(ResourceTable.String_toolbar_color), getContext().getString(ResourceTable.String_toolbar_color_details));
                break;
            case ResourceTable.Id_textAllCapsInfoButton:
                showDialog(getContext().getString(ResourceTable.String_text_all_caps), getContext().getString(ResourceTable.String_text_all_caps_details));
                break;
            case ResourceTable.Id_underlineColorInfoButton:
                showDialog(getContext().getString(ResourceTable.String_underline_color), getContext().getString(ResourceTable.String_underline_color_details));
                break;
            case ResourceTable.Id_underlineHeightInfoButton:
                showDialog(getContext().getString(ResourceTable.String_underline_height), getContext().getString(ResourceTable.String_underline_height_details));
                break;
            case ResourceTable.Id_indicatorColorInfoButton:
                showDialog(getContext().getString(ResourceTable.String_indicator_color), getContext().getString(ResourceTable.String_indicator_color_details));
                break;
            case ResourceTable.Id_indicatorHeightInfoButton:
                showDialog(getContext().getString(ResourceTable.String_indicator_height), getContext().getString(ResourceTable.String_indicator_height_details));
                break;
            case ResourceTable.Id_paddingMiddleInfoButton:
                showDialog(getContext().getString(ResourceTable.String_padding_middle), getContext().getString(ResourceTable.String_padding_middle_details));
                break;
            case ResourceTable.Id_sameWeighTabsInfoButton:
                showDialog(getContext().getString(ResourceTable.String_same_weight_tabs), getContext().getString(ResourceTable.String_same_weight_tabs_details));
                break;
            case ResourceTable.Id_showToolbarInfoButton:
                showDialog(getContext().getString(ResourceTable.String_show_toolbar), getContext().getString(ResourceTable.String_show_toolbar_details));
                break;
            case ResourceTable.Id_numberOfTabsInfoButton:
                showDialog(getContext().getString(ResourceTable.String_number_of_tabs), getContext().getString(ResourceTable.String_number_of_tabs_details));
                break;
            default:
                break;
        }
    }

    private void showDialog(String title, String content) {
        CustomDialog.init(getContext())
                .setLayoutId(ResourceTable.Layout_dialog_info_layout, ConstandCodes.COMMONTYPE)
                .setViewConvertListener((holder, dialog) -> {
                    holder.getView(ResourceTable.Id_yes_or_no_layout).setVisibility(Component.HIDE);
                    Text tv_title = holder.getView(ResourceTable.Id_tv_title);
                    tv_title.setText(title);
                    Text tv_content = holder.getView(ResourceTable.Id_tv_content);
                    tv_content.setText(content);
                })
                .setSwipeToClose(true)
                .setTransparent(true)
                .show();
    }
}
